using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace InfoExpress.WinApp.Utils
{
  public class Logger
  {
    public static bool IsDebug = true;

    /// <summary>
    /// Log Error 
    /// </summary>
    /// <param name="message"></param>
    /// <param name="ex"></param>
    /// <param name="category"></param>
    /// <param name="properties"></param>
    public static void LogError(string message, Exception ex, string category, IDictionary<string, object> properties)
    {
      Log(message, ex, category, properties, System.Diagnostics.TraceEventType.Error);
    }

    /// <summary>
    /// Log Warning
    /// </summary>
    /// <param name="message"></param>
    /// <param name="ex"></param>
    /// <param name="category"></param>
    /// <param name="properties"></param>
    public static void LogWarning(string message, Exception ex, string category, IDictionary<string, object> properties)
    {
      Log(message, ex, category, properties, System.Diagnostics.TraceEventType.Warning);
    }

    /// <summary>
    /// Log Information
    /// </summary>
    /// <param name="message"></param>
    /// <param name="ex"></param>
    /// <param name="category"></param>
    /// <param name="properties"></param>
    public static void LogInformation(string message, Exception ex, string category, IDictionary<string, object> properties)
    {
      Log(message, ex, category, properties, System.Diagnostics.TraceEventType.Information);
    }

    /// <summary>
    /// Log Critical
    /// </summary>
    /// <param name="message"></param>
    /// <param name="ex"></param>
    /// <param name="category"></param>
    /// <param name="properties"></param>
    public static void LogCritical(string message, Exception ex, string category, IDictionary<string, object> properties)
    {
      Log(message, ex, category, properties, System.Diagnostics.TraceEventType.Critical);
    }

    /// <summary>
    /// Log Verbose
    /// </summary>
    /// <param name="message"></param>
    /// <param name="ex"></param>
    /// <param name="category"></param>
    /// <param name="properties"></param>
    public static void LogVerbose(string message, Exception ex, string category, IDictionary<string, object> properties)
    {
      if (IsDebug)
        Log(message, ex, category, properties, System.Diagnostics.TraceEventType.Verbose);
    }

    /// <summary>
    /// General Log 
    /// </summary>
    /// <param name="message"></param>
    /// <param name="ex"></param>
    /// <param name="category"></param>
    /// <param name="properties"></param>
    /// <param name="traceEventType"></param>
    public static void Log(string message, Exception ex, string category, IDictionary<string, object> properties,
      System.Diagnostics.TraceEventType traceEventType)
    {
      LogEntry log = new LogEntry();
      log.Message = message;
      if (ex != null)
        log.AddErrorMessage(ex.ToString());
      if (!string.IsNullOrEmpty(log.ErrorMessages))
        log.Message += " : " + log.ErrorMessages;
      log.Categories.Add(category.ToString());
      log.Severity = traceEventType;
      log.ExtendedProperties = properties;
      Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(log);
    }
  }
}
